﻿using Decoration.IService;
using Decoration.Models;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DictTypeController : ControllerBase
    {
        IDictTypeServict svc;
        public DictTypeController(IDictTypeServict svc)
        {
          this.svc = svc;
        }

        // GET: api/<DictTypeController>
        [HttpGet]
        public PagedResult<Base_DictType> Get(string? dictTypeName,int pageindex=1,int pagesize=10)
        {
            var data = svc.Query(o=>true);
            if (!string.IsNullOrEmpty(dictTypeName)) 
            {
                data = data.Where(o=>o.DictTypeName.Contains(dictTypeName));
            }

            int total = data.Count();
            data = data.OrderByDescending(o => o.CreateTime).Skip(pagesize * (pageindex - 1)).Take(pagesize);

            return new PagedResult<Base_DictType>() {code=1,msg="成功",data=data,total=total };
        }

        // GET api/<DictTypeController>/5
        [HttpGet("{id}")]
        public Result Get(int id)
        {
                var info = svc.Get(o=>o.Id==id);
                if (info == null)
                {
                    return new Result() { code = -1, msg = "为查询到数据" };
                }
                return new Result() { code = 1, msg = "成功", data = info };
        }

        // POST api/<DictTypeController>
        [HttpPost]
        public Result Post(Base_DictType type)
        {
            var info = new Base_DictType() { 
               CreateTime = DateTime.Now,
               Status = type.Status,
               DictTypeName = type.DictTypeName,              
            };
            if (info == null)
            {
                return new Result() { code = -1, msg = "数据不能为空" };
            }
            var res = svc.Add(info);
            return new Result() { code = 1,msg="成功",data=res };

        }

        // PUT api/<DictTypeController>/5
        [HttpPut]
        public Result Put(Base_DictType type)
        {
            var getId = svc.Get(o=>o.Id==type.Id);
            if (getId == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            getId.Status = type.Status;
            getId.DictTypeName = type.DictTypeName;
            var res=svc.Update(getId);
            return new Result() { code = 1, msg = "成功", data = res };
        }

        // DELETE api/<DictTypeController>/5
        [HttpDelete("{id}")]
        public Result Delete(int id)
        {
            var getId = svc.Get(o => o.Id ==id);
            if (getId == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            getId.Status = 0;
            var res = svc.Update(getId);
            return new Result() { code = 1, msg = "成功", data = res };

        }
        [HttpGet]
        [Route("TypeName")]
        public Result TypeName()
        {
            var data = svc.Query(o => o.Status == 1);
            return new Result() { code = 1, data = data };
        }
    }
}
